package com.itlong.cloud.property.service.impl;

import com.itlong.cloud.POJO.DTO.property.PropertyVisitorPassRecordGetDTO;
import com.itlong.cloud.POJO.VO.property.PropertyVisitorPassRecordGetVO;
import com.itlong.cloud.property.dao.IVisitorPassRecordDao;
import com.itlong.cloud.property.service.IPropertyVisitorPassRecordService;
import com.itlong.cloud.utils.data.handler.SqlUtil;
import com.itlong.cloud.utils.page.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <desc>
 *      通行记录实现类
 * </desc>
 *
 * @createDate 2018/06/27
 */
@Service
public class PropertyVisitorPassRecordServiceImpl implements IPropertyVisitorPassRecordService {


    //访客通行记录Dao
    @Autowired
    IVisitorPassRecordDao iVisitorPassRecordDao;


    /**
     * <desc>
     *     访客通行记录列表
     * <desc/>
     *
     *@param  visitorPassRecordGetDTO  分页查询访客通行记录
     * @return  MessageVO 标准格式化响应结果
     * @author JuGuang.S
     * @createDate 2018/06/27
     */
    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public Page<PropertyVisitorPassRecordGetVO> getVisitorPassRecordByPage(PropertyVisitorPassRecordGetDTO visitorPassRecordGetDTO) throws Exception {
        Map<String, Object> params = SqlUtil.convertBeanToMapParams(visitorPassRecordGetDTO);
        params.put("projectId", visitorPassRecordGetDTO.getProjectId());

        Page<PropertyVisitorPassRecordGetVO> page = new Page<>();
        page.getPage().setPageSize(visitorPassRecordGetDTO.getPageSize());
        page.getPage().setCurrentPage(visitorPassRecordGetDTO.getCurrentPage());
        params.put("currentPage", page.getPage().getStartSlip());
        params.put("pageSize", page.getPage().getPageSize());

        List<PropertyVisitorPassRecordGetVO> recordList = iVisitorPassRecordDao.getVisitorPassRecordByPage(params);
        Integer recordCount = iVisitorPassRecordDao.getVisitorPassRecordCount(params);
        recordList.forEach(vo->{
            if(StringUtils.isNotBlank(vo.getVisitorPhone())&&vo.getVisitorPhone().length() >= 11){
                vo.setVisitorPhone(vo.getVisitorPhone().substring(0,3)+"****"+vo.getVisitorPhone().substring(7,11));
            }
        });
        page.setRows(recordList, recordCount);
        return page;
    }

}